package com.prestamomo.vil.ppa.pmvilmedium.pmvilarm.pmsave;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;

import com.prestamomo.vil.ppa.PmvilApp;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PmvilStorageMd {


    public static JSONObject getInfoPmvilStorage() throws JSONException {
        JSONObject storagePmvilObj = new JSONObject();
        storagePmvilObj.put("awgmauh", getTotalPmvilMemory(PmvilApp.getPmvilApss()));//ram_total_size

        try {
            ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
            ActivityManager activityManager = (ActivityManager) PmvilApp.getPmvilApss().getSystemService(Context.ACTIVITY_SERVICE);
            if (activityManager != null) {
                activityManager.getMemoryInfo(memoryInfo);
                storagePmvilObj.put("hmgdvz", memoryInfo.availMem);//ram_usable_size
            }
        } catch (Exception e) {
            storagePmvilObj.put("hmgdvz", 0);//ram_usable_size
        }
        storagePmvilObj.put("qibwsgkwf", getFsTotalPmvilSize(getSDCardPmvilPath()) == 0
                ? -1 : getFsTotalPmvilSize(getSDCardPmvilPath()));//memory_card_size
        storagePmvilObj.put("hubei",
                getFsTotalPmvilSize(getSDCardPmvilPath()) - getFsAvailablePmvilSize(getSDCardPmvilPath()));//memory_card_size_use

        try {
            File pmvilFile = Environment.getDataDirectory();
            StatFs pmvilStatFse = new StatFs(pmvilFile.getPath());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                storagePmvilObj.put("otbrwvb", pmvilStatFse.getBlockSizeLong() * pmvilStatFse.getAvailableBlocksLong());//internal_storage_usable
            } else {
                storagePmvilObj.put("otbrwvb", pmvilStatFse.getBlockSize() * pmvilStatFse.getAvailableBlocks());//internal_storage_usable
            }
        } catch (Exception e) {
            storagePmvilObj.put("otbrwvb", 0);//internal_storage_usable
        }


        storagePmvilObj.put("drinkszs", getTotalPmvilStorage());//internal_storage_total
        try {
            storagePmvilObj.put("ymjxiaax", PmvilApp.getPmvilApss().getCacheDir().getParentFile().getAbsolutePath());//main_storage
        } catch (Exception e) {
            storagePmvilObj.put("ymjxiaax", "");//main_storage
        }

        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                storagePmvilObj.put("wkzmv", Environment.getExternalStorageDirectory().getAbsolutePath());//external_storage
            } else {
                storagePmvilObj.put("wkzmv", PmvilApp.getPmvilApss()
                        .getExternalFilesDir(null).getParentFile().getParentFile().getAbsolutePath());//external_storage
            }
        } catch (Exception e) {
            storagePmvilObj.put("wkzmv", "");//external_storage
        }

        return storagePmvilObj;
    }


    private static long getTotalPmvilMemory(Context pmvilCtext) {
        try {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                try {
                    File pmvilFile = new File("/proc/meminfo");
                    FileReader filePmvilReader = new FileReader(pmvilFile);
                    BufferedReader bufferedPmvilReader = new BufferedReader(filePmvilReader, 8192);
                    String totalPmvilMemoryLine = bufferedPmvilReader.readLine();
                    Matcher pmvilMatcher = Pattern.compile("(\\d+)").matcher(totalPmvilMemoryLine);
                    if (pmvilMatcher.find()) {
                        return Long.parseLong(pmvilMatcher.group(1)) * 1024;
                    }
                    bufferedPmvilReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {
                ActivityManager.MemoryInfo memoryPmvilInfo = new ActivityManager.MemoryInfo();
                ActivityManager atyPmvilMager = (ActivityManager) pmvilCtext.getSystemService(Context.ACTIVITY_SERVICE);
                if (atyPmvilMager != null) {
                    atyPmvilMager.getMemoryInfo(memoryPmvilInfo);
                    return memoryPmvilInfo.totalMem;
                } else {
                    return 0;
                }
            }
        } catch (Exception e) {
            return 0;
        }
        return 0;
    }


    private static long getTotalPmvilStorage() {
        try {
            File pmvilPath = Environment.getDataDirectory();
            StatFs pmvilStatFs = new StatFs(pmvilPath.getPath());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                return pmvilStatFs.getBlockSizeLong() * pmvilStatFs.getBlockCountLong();
            } else {
                return pmvilStatFs.getBlockSize() * pmvilStatFs.getBlockCount();
            }
        } catch (Exception e) {
            return 0;
        }
    }

    private static long getFsTotalPmvilSize(String pmvilPathInFs) {
        if (TextUtils.isEmpty(pmvilPathInFs)) return 0;
        try {
            StatFs statFsPrTols = new StatFs(pmvilPathInFs);
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? statFsPrTols.getBlockSizeLong() * statFsPrTols.getBlockCountLong() : statFsPrTols.getBlockSize() * statFsPrTols.getBlockCount();
        } catch (Exception e) {
            return 0;
        }

    }

    private static String getSDCardPmvilPath() {
        try {
            return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ? Environment.getExternalStorageDirectory().getAbsolutePath() : "";
        } catch (Exception e) {
            return "";
        }
    }

    private static long getFsAvailablePmvilSize(final String pmvilPathInFs) {
        if (TextUtils.isEmpty(pmvilPathInFs)) return 0;
        try {
            StatFs statFsPrTols = new StatFs(pmvilPathInFs);
            return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 ? statFsPrTols.getBlockSizeLong() * statFsPrTols.getAvailableBlocksLong() : statFsPrTols.getBlockSize() * statFsPrTols.getAvailableBlocks();
        } catch (Exception e) {
            return 0;
        }
    }
}
